﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using UtilityLibrary.WinControls;
using V5_Apps.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace V5_Apps {
    public partial class frmMain : Form {
        private frmTreeBox treeBox = new frmTreeBox();
        private frmOutBox outBox = new frmOutBox();
        private DeserializeDockContent m_deserializeDockContent;

        public frmMain() {

            InitializeComponent();

            dockPanel.Dock = DockStyle.Fill;

            m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);

            treeBox.Show(dockPanel, DockState.DockLeft);

            outBox.Show(dockPanel, DockState.DockBottom);


            treeBox.OutTNO = delegate(string pluginInfo) {
                string[] plugin = pluginInfo.Split(new string[] { "||" }, StringSplitOptions.None);
                string Text = plugin[0];
                string Dir = plugin[1];
                string Commond = plugin[2];
                string RunTime = plugin[3];

                LoadSinglePlugin(Text, Dir, Commond, RunTime);
            };
        }


        private IDockContent GetContentFromPersistString(string persistString) {
            if (persistString == typeof(frmTreeBox).ToString()) {
                return treeBox;
            }
            if (persistString == typeof(frmOutBox).ToString()) {
                return outBox;
            }
            else {
                return null;
            }
        }

        private void SaveDockXml() {
            string configDir = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config");
            string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config/DockPanel.config");
            if (!Directory.Exists(configDir)) {
                Directory.CreateDirectory(configDir);
            }
            if (!File.Exists(configFile)) {
                File.Create(configFile).Close();
            }
            dockPanel.SaveAsXml(configFile);
        }

        private void LoadDockXml() {
            try {
                string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config/DockPanel.config");
                if (File.Exists(configFile)) {
                    dockPanel.LoadFromXml(configFile, m_deserializeDockContent);
                }
            }
            catch (Exception) {
            }

        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e) {
            SaveDockXml();
        }

        private void frmMain_Load(object sender, EventArgs e) {
            LoadDockXml();
        }

        private void LoadSinglePlugin(string Text,
                string Dir,
                string Commond,
                string RunTime) {
            this.ToolStripMenuItem_Plugin.DropDownItems.Clear();
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            Assembly asm = Assembly.LoadFrom(basePath + "Plugins\\" + Dir + "\\" + RunTime);
            Type tp = asm.GetType(Commond);
            Object objects = Activator.CreateInstance(tp);
            //添加窗体
            Form my = objects as Form;
            DockContent frm = my as DockContent;
            bool isOpen = true;
            foreach (var doc in dockPanel.Documents) {
                if (doc.DockHandler.TabText == frm.Text) {
                    doc.DockHandler.Activate();
                    isOpen = false;
                    break;
                }
            }
            if (isOpen) {
                frm.Show(dockPanel, DockState.Document);

            }
            #region 菜单
            try {
                ToolStripMenuItem[] menus = (ToolStripMenuItem[])tp.GetMethod("GetMenuItem").Invoke(objects, new object[] { });
                ToolStripMenuItem tsm = new ToolStripMenuItem(Text);
                tsm.DropDownItems.AddRange(menus);
                ToolStripMenuItem[] menuPlugin = { tsm };
                this.ToolStripMenuItem_Plugin.DropDownItems.AddRange(menuPlugin);
            }
            catch (Exception) {

            }
            #endregion
            /*
            //加载插件工具栏
            ToolStripItem[] tsItems = (ToolStripItem[])Fype.GetMethod("GetToolBarItem").Invoke(objects, null);
            toolStrip1.Items.AddRange(tsItems);

            //加载状态栏
            ToolStripItem[] tsStatus = (ToolStripItem[])Fype.GetMethod("GetStatusItem").Invoke(objects, null);
            statusStrip1.Items.AddRange(tsStatus);
            */
        }


        private void ToolStripMenuItem_File_Exit_Click(object sender, EventArgs e) {
            System.Environment.Exit(0);
        }

    }
}
